import {assign} from "min-dash";
import {getDi} from "bpmn-js/lib/util/ModelUtil";

export default function PaletteProvider(palette, create, elementFactory, globalConnect) {
    this.create = create
    this.elementFactory = elementFactory
    this.globalConnect = globalConnect

    palette.registerProvider(this)
}

PaletteProvider.$inject = [
    'palette',
    'create',
    'elementFactory',
    'globalConnect'
]

PaletteProvider.prototype.getPaletteEntries = function() {
    const {
        create,
        elementFactory,
        globalConnect
    } = this;

    function createAction(type, group, className, title, options) {
        function createListener(event) {
            const shape = elementFactory.createShape(assign({ type: type }, options));
            if (options) {
                const di = getDi(shape);
                di.isExpanded = options.isExpanded;
            }
            create.start(event, shape);
        }

        // const shortType = type.replace(/^bpmn:/, '');
        return {
            group: group,
            className: className,
            title: '创建'+ title,
            action: {
                dragstart: createListener,
                click: createListener
            }
        };
    }

    return {
        'global-connect-tool': {
            group: 'tools',
            className: 'bpmn-icon-connection-multi',
            title: '创建箭头',
            action: {
                click: function(event) {
                    globalConnect.start(event);
                }
            }
        },
        'create.start-event': createAction(
            'bpmn:StartEvent', 'event', 'bpmn-icon-start-event-none',
            '开始事件'
        ),
        'create.task': createAction(
            'bpmn:UserTask', 'activity', 'bpmn-icon-user-task',
            '任务节点'
        ),
        'create.exclusive-gateway': createAction(
            'bpmn:ExclusiveGateway', 'gateway', 'bpmn-icon-gateway-none',
            '网关'
        ),
        'create.end-event': createAction(
            'bpmn:EndEvent', 'event', 'bpmn-icon-end-event-none',
            '结束事件'
        ),
    }
}
